package com.sundablog.demo.action;

/**
 * @Title:
 * @Package com.sundablog.demo.action
 * @Description: TODO
 * @author: 孙达
 * @date: 2020/04/01 17:37
 * @Copyright: All rights Reserved, Designed By www.sundablog.com
 */

import com.sundablog.demo.service.IDemoService;
import com.sundablog.mvc.annotation.SDAutowired;
import com.sundablog.mvc.annotation.SDController;
import com.sundablog.mvc.annotation.SDRequestMapping;
import com.sundablog.mvc.annotation.SDRequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@SDController
@SDRequestMapping("/demo")
public class DemoAction {

    @SDAutowired
    private IDemoService demoService;

    @SDRequestMapping("/query")
    public void query(HttpServletRequest req, HttpServletResponse resp,
                      @SDRequestParam("name") String name){
        String result = demoService.get(name);
        try {
            resp.getWriter().write(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @SDRequestMapping("/add")
    public void add(HttpServletRequest req, HttpServletResponse resp,
                    @SDRequestParam("a") Integer a, @SDRequestParam("b") Integer b){
        try {
            resp.getWriter().write(a + "+" + b + "=" + (a + b));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @SDRequestMapping("/sub")
    public void add(HttpServletRequest req, HttpServletResponse resp,
                    @SDRequestParam("a") Double a, @SDRequestParam("b") Double b){
        try {
            resp.getWriter().write(a + "-" + b + "=" + (a - b));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @SDRequestMapping("/remove")
    public String  remove(@SDRequestParam("id") Integer id){
        return "" + id;
    }

}
